package spring.aopLtw;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.aopLtw.ltw.DemoDaoImpl;

/**
 * AOP学习: 加载时织入
 *      -javaagent:E:/Repos/maven/org/springframework/spring-instrument/5.1.7.RELEASE/spring-instrument-5.1.7.RELEASE.jar
 *
 * @author ME
 */
public class TestSpringAopLTW {

    public static void main(String[] args) throws Exception {
        // ApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
        // TODO javaConfig 配置有 bug，不生效
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-ltw.xml", AopConfig.class);

        // 脱离 Spring 使用
        System.out.println("\n");
        DemoDaoImpl dao = new DemoDaoImpl();
        dao.run2();

        DemoDaoImpl daoSpring = (DemoDaoImpl) context.getBean("demoDaoImpl");
        daoSpring.run();
    }

    @Configuration
    @ComponentScan("spring.aopLtw")
    @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
    public static class AopConfig {
        // @Bean
        public DemoDaoImpl demoDaoImpl() {
            return new DemoDaoImpl();
        }
    }

}
